<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Clickframes {

	/**
	 *	Helper method for sending an e-mail through CodeIgniter's email library.
	 *
	 *	@param string $from Sender's e-mail address
	 *	@param string $to Recipient's e-mail address
	 *	@param string $subject Message subject
	 *	@param string $template Message body template.
	 *	@param array $vars Keys of array appearing in template will be replaced with associated values.
	 *	@return boolean Returns true if message sent successfully.
	 */
	function sendMessage($from, $to, $subject, $template, $vars) {

		$CI =& get_instance();
		$CI->load->library('email');

		$CI->email->clear(TRUE);

		$CI->email->from($from);
		$CI->email->to($to);
		$CI->email->subject($subject);
		$CI->email->message(str_replace(array_keys($vars), array_values($vars), $template));

		if (!$CI->email->send()) {
			return $CI->email->print_debugger();
		}

		return TRUE;
	}
	
	/**
	 *	Truncates a string to maximum length specified. If truncated,
	 *	string ends with ellipsis.
	 *	
	 *	@param string $string String to truncate
	 *	@param int $maxLength Maxmimum number of characters to return, including elipsis, if applicable
	 *	@return string Truncated string, or original string if under maxmimum length
	 */
	function truncate($string, $maxLength) {
		
		if (strlen($string) > $maxLength) {
			$string = substr($string, 0, ($maxLength - 3)) . '...';
		}
		
		return $string;
		
	}

	/**
	 *	Confirms if session is alive. Default timeout of 900 seconds.
	 *	
	 *	@return boolean
	 */
	function sessionAlive() {

		$CI =& get_instance();
		$CI->load->library('phpsession');

		return ($CI->session->userdata('username') !== FALSE
				&& $CI->session->userdata('last_activity') > (time() - 900));

	}
	
	/**
	 *	Convert a timestamp (UTC) into a formatted local time.
	 *	
	 *	@param int $timestamp Unix timestamp (UTC)
	 *	@param string $timezone Timezone in Area/Location format, e.g. America/New_York
	 *	@param string $format Desired string date format
	 *	@return string Formatted date in local timezone.
	 */
	function formatLocalTime($timestamp, $timezone, $format = 'j M Y &\m\i\d\d\o\t; g:iA') {
		$zone = new DateTimeZone($timezone);
		$datetime = new DateTime();
		$datetime->setTimestamp($timestamp);
		$datetime->setTimezone($zone);
		return $datetime->format($format);
	}
	
	function markupTwitterStatus($status, $noFollow = true) {
		
		$rel = '';
		if ($noFollow) {
			$rel = 'rel="nofollow"';
		}
		
		// convert link to url
		$status = preg_replace("/((http:\/\/|https:\/\/)[^ )\r\n]+)/i", "<a href=\"$1\" $rel >$1</a>", $status);
	
		// convert @ to follow
		$status = preg_replace("/(@([_a-z0-9\-]+))/i", "<a href=\"http://twitter.com/$2\" $rel >$1</a>", $status);
	
		// convert # to search
		$status = preg_replace("/(#([_a-z0-9\-]+))/i", "<a href=\"http://search.twitter.com/search?q=%23$2\" $rel >$1</a>", $status);
		
		return $status;
	}

}

/* clickframes::version=222672484::clickframes */
?>